package com.aixx.springcloud.controller;

import cn.hutool.core.util.IdUtil;
import com.aixx.springcloud.Service.Paymentservice;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author shkstart
 * @create 2021-01-29 8:01
 */
@RestController
@Slf4j
//全局配置降级方法的注解
@DefaultProperties(defaultFallback ="paymentInfo_timeoutHandler")
public class PaymentController {
    @Resource
    private Paymentservice paymentService;


//全局降级

    @HystrixCommand
    @GetMapping("/payment/hystrix/{id}")
    public String paymentInfo_OK(@PathVariable("id")Integer id){
        int i =10/0;
        log.info("paymentInfo_OKKKKOKKK");
        return paymentService.paymentinfo_Ok(id);
    }

    //兜底方法，注意，兜底方法参数随意
    public String paymentInfo_timeoutHandler(){
        log.info("paymentInfo_timeout--handler");
        return "访问 payment 失败----人工报错+";
    }



    //    服务降级
//    主启动类添加注解： @EnableCircuitBreaker
    @HystrixCommand(fallbackMethod ="paymentInfo", commandProperties = {
            //设置峰值，超过 3 秒，就会调用兜底方法
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value = "5000")
    })
    @GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentInfo_Timeout(@PathVariable("id")Integer id){
        log.info("paymentInfo_timeout");
        return paymentService.paymentinfo_Timeout(id);
    }
    //兜底方法，注意，兜底方法参数随意 参数必须一致
    public String paymentInfo(Integer id){
        log.info("paymentInfo_timeout--handler");
        return "访问 payment 失败----人工报错+"+id;
    }

    //====服务熔断
    @GetMapping("/payment/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id")Integer id){
        return paymentService.paymentCircuitBreaker(id);
    }
}
